//
//  CustomView.m
//  text
//
//  Created by apple on 15/9/11.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "CustomView.h"

@interface CustomView ()
{
    BOOL _needMove;
    CGPoint _firstPoint;
}

@end

@implementation CustomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        CGSize size = frame.size;
        
        
        //滑动条
        _aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width * 0.1, size.height)];
        
        [self addSubview:_aLabel];
        
        // 进度
        _bLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, size.height * 0.2, 0, size.height * 0.6)];
        
        [self addSubview:_bLabel];
        
        //剩余进程
        
        _cLabel = [[UILabel alloc] initWithFrame:CGRectMake(size.width * 0.1, size.height * 0.2, size.width * 0.9, size.height * 0.6)];
        
        [self addSubview:_cLabel];
    }
    
    return  self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    _firstPoint = [touch locationInView:self];
    

    
    if (CGRectContainsPoint(_aLabel.frame, _firstPoint))
    {
        _needMove = YES;
    }
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    
    _point = [touch locationInView:self];
    
    CGSize size = self.frame.size;
    
    CGFloat dx = _point.x - _firstPoint.x;
    
    if (CGRectContainsPoint(_aLabel.frame, _point))
    {
        if (0 <= _point.x && _point.x <= self.frame.size.width * 0.9)
        {
            _aLabel.frame = CGRectMake( self.frame.origin.x + dx,0,size.width * 0.1,size.height);
    
            _bLabel.frame = CGRectMake(0,size.height * 0.2,dx,size.height * 0.6);
    
            _cLabel.frame = CGRectMake(dx + size.width * 0.1,size.height * 0.2,size.width - (_point.x - _firstPoint.x + size.width * 0.1),size.height * 0.6);
            
//            _value = (_point.x + size.width * 0.1) / size.width;

            _value = _point.x / (size.width * 0.9);
        }
    }
    if (_valueChanged)
    {
        _valueChanged(self);
    }

}
@end
